package remote;

import java.io.Serializable;
import java.rmi.RemoteException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import log.Logger;
/**
 * 
 * @author Team4
 * @author Daniel Zimmerman - modified the blockingGet()
 * @version Spring 2013
 *
 */
public class FutureMessage implements RemoteFuture,Serializable
{
  /**
   * 
   * @throws RemoteException
   */
  public FutureMessage(String the_key) throws RemoteException
  {
    this.my_key = the_key;
  }
  /**
   * The string key
   */
  private String my_key;
  /**
   * value.
   */
  private Serializable my_value;
  /**
   * boolean to indicate if it is done.   
   */
  private boolean my_done = false;
  
  @Override
  public boolean isDone()
  {
    return my_done;
  }

  public Serializable blockingGet()
  {
    synchronized (this) 
    {
      while (!my_done)
      {
        try
        {
          wait();

        }
        catch (InterruptedException e)
        {
          //oh well
        }
      }
    }
    return my_value;
  }
  
  public void set(Serializable object) throws RemoteException
  {
    synchronized (this)
    {
      my_value = object;
      my_done = true;
      notifyAll();
    }
  }
}
